/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import "../../../../shared/qml" as Shared
import "../../../../shared/qml/UIConstants.js" as UIConstants
import "../misc" as Misc
import NewsBlur 1.0

Item {
    id: root

    signal released(int row)
    signal holded(int row)

    property int titleSize: UIConstants.LIST_TILE_SIZE
    property int titleWeight: Font.Bold
    property color titleColor: UIConstants.LIST_TITLE_COLOR

    property int subtitleSize: UIConstants.LIST_SUBTILE_SIZE
    property int subtitleWeight: Font.Light
    property color subtitleColor: UIConstants.LIST_SUBTITLE_COLOR
    property bool __alternativeUnreadCountDisplay: ((model.itemType == NewsBlurItem.FolderItem)
                                                    || (model.itemType == NewsBlurItem.EverythingGroup)
                                                    || (model.itemType == NewsBlurItem.StarredGroup))

    height: UIConstants.DEFAULT_DELEGATE_HEIGHT
    width: parent.width

    BorderImage {
        id: background
        anchors.fill: parent
        // Fill page porders
        anchors.leftMargin: -UIConstants.DEFAULT_MARGIN
        anchors.rightMargin: -UIConstants.DEFAULT_MARGIN
        visible: mouseArea.pressed
        source: "image://theme/meegotouch-panel-background-pressed"
    }

    Component {
        id: progressBarComponenet
        Shared.ProgressBar {
            indeterminate: true
        }
    }

    Shared.Separator {
        id: separator
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.leftMargin: UIConstants.DEFAULT_MARGIN
        anchors.right: parent.right
        anchors.rightMargin: UIConstants.DEFAULT_MARGIN
        visible: index
    }
    Item {
        id: conents
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.leftMargin: UIConstants.DEFAULT_MARGIN
        anchors.right: parent.right
        anchors.rightMargin: UIConstants.DEFAULT_MARGIN
        Shared.FeedIcon {
            id: iconImage
            anchors.left: parent.left
            anchors.verticalCenter: parent.verticalCenter
            visible: model.iconSource
            source: model.iconSource
            isFeedIcon: (model.itemType == NewsBlurItem.FeedItem)
        }
        Column {
            anchors.left: iconImage.right
            anchors.leftMargin: UIConstants.LIST_ITEM_SPACING
            anchors.right: rightRow.left
            anchors.rightMargin: UIConstants.LIST_ITEM_SPACING
            anchors.verticalCenter: parent.verticalCenter
            Text {
                id: mainText

                text: model.title
                font.family: UIConstants.FONT_FAMILY
                font.weight: root.titleWeight
                font.pixelSize: root.titleSize
                color: root.titleColor
                width: parent.width
                elide: Text.ElideRight
                wrapMode: Text.Wrap
                maximumLineCount: root.__alternativeUnreadCountDisplay ? 1 : 2
                textFormat: Text.PlainText
            }
            Text {
                id: unreadCountText

                text: root.__alternativeUnreadCountDisplay ? model.unreadCountText : ""
                font.pixelSize: UIConstants.FONT_LSMALL
                font.family: UIConstants.FONT_FAMILY_LIGHT
                color: "dimgray"
                elide: Text.ElideRight
                maximumLineCount: 1
                width: parent.width
                visible: root.__alternativeUnreadCountDisplay && unreadCountText.text.length
            }
            Loader {
                sourceComponent: model.movingBusy || model.deletingBusy ? progressBarComponenet : undefined;                
                width: parent.width
            }
        }
        Row {
            id: rightRow
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            Misc.ClassificationCountBubble {
                id: countBubble
                anchors.verticalCenter: parent.verticalCenter
                neutralCount: model.unreadCount
                positiveCount: model.posUnreadCount
                negativeCount: model.negUnreadCount
                visible: !root.__alternativeUnreadCountDisplay
            }
            MoreIndicator {
                id: moreIndicator
                anchors.verticalCenter: parent.verticalCenter
                visible: root.__alternativeUnreadCountDisplay
            }
        }
    }
    MouseArea {
        id: mouseArea;
        anchors.fill: parent
        onReleased: {
            if(!mouse.wasHeld && (!model.movingBusy && !model.deletingBusy))
                root.released(index);
        }
        onPressAndHold: {
            if(!model.movingBusy && !model.deletingBusy)
                root.holded(index);
        }
    }
}
